package com.jams.goodox.utils;

import android.content.Context;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.entity.UpLoadPicBean;
import com.jams.goodox.listen.UploadListener;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.LifeService;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import uama.com.image.compress.ImageCompressFactory;

/**
 * Created by jams on 2017/4/19.
 * ***
 * Tel: ***
 *
 * Retrofit 工具类
 */

public class RetrofitUtils {

    public static void uploadImage(final Context context, final List<String> paths, final String type, final UploadListener uploadListener) {
        if(!(context instanceof BaseActivity)){
            if(uploadListener != null){
                uploadListener.failure();
            }
            return;
        }
        final  List<MultipartBody.Part> partList = new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (paths.size() > 0) {
                    for (int i = 0; i < paths.size(); i++) {
//                        File imgFile = UImageUtils.sword(paths.get(i));
                        File imgFile = ImageCompressFactory.getNewFile(context,paths.get(i));
                        if(imgFile != null){
                            partList.add(MultipartBody.Part.createFormData("files", imgFile.getName(), RequestBody.create(MediaType.parse("image/png"), imgFile)));
                        }
                    }
                }
                if(partList.size() == 0){
                    uploadListener.failure();
                    return;
                }
                ((BaseActivity)context).cancelAllCall();
                ((BaseActivity)context).enqueue(((BaseActivity) context).createCallService(LifeService.class).uploadImages(type,partList), new RetrofitCallbackListener<UpLoadPicBean>() {
                    @Override
                    public void onSuccess(Call<UpLoadPicBean> call, UpLoadPicBean result) {
                        uploadListener.success(result.getData());
//                        UImageUtils.deleteAllFiles(new File(UImageUtils.Path));
                        ImageCompressFactory.clearCacheFiles(context);
                    }

                    @Override
                    public void onError(String msg) {
                        uploadListener.failure();
//                        UImageUtils.deleteAllFiles(new File(UImageUtils.Path));
                        ImageCompressFactory.clearCacheFiles(context);
//                        Log.i("RetrofitUtils ", "uploadImage() error:" + msg);
                    }
                });
            }
        }).start();

    }

    /**
     * 主动取消Retrofit call 请求
     *
     * @param call 需要处理的call
     */
    public static void cancelCallExecuted(Call call){
        if(call != null && call.isExecuted()){
            call.cancel();
        }
    }

    public static Map<String, RequestBody> getFileMap(List<String> paths, String fileType) {
        final Map<String, RequestBody> photos = new HashMap<>();
        if (paths.size() > 0) {
            for (int i = 0; i < paths.size(); i++) {
                File imgFile = UImageUtils.sword(paths.get(i));
                RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), imgFile);
                photos.put(fileType + "\"; filename=\"" + imgFile.getName(), fileBody);
            }
        }
        return photos;
    }
}
